<template>
	<view class="container">
		<view>
			<conversationCell @click='conversationCellTap(item, index)' :item='item' v-for='(item, index) in conversationList' :key='index'></conversationCell>
		</view>
		<view style="padding-top: 300rpx;" v-if='showEmpty'>
			<u-empty text="暂无数据" mode="list"></u-empty>
		</view>
	</view>
</template>

<script>
	import {
		user
	} from '@/api/user.js'
	import {message} from '@/api/message.js'
	import conversationCell from '@/components/conversationCell/conversationCell.vue'
	export default{
		components:{conversationCell},
		data() {
			return {
				page: 1,
				size: 20,
				showEmpty: false,
				conversationList:[],
				sys_conversation:{},
				userInfo: {},
			}
		},
		onLoad() {
			
		},
		onShow() {
			this.page = 1
			this.getConversationList()
			//设置角标
			var userInfo = this.$store.state.userInfo
			if(userInfo.unreadMessage>0){
				if(userInfo.unreadMessage > 99){
					userInfo.unreadMessage = '99+'
				}else{
					userInfo.unreadMessage = String(userInfo.unreadMessage)
				}
				uni.setTabBarBadge({
				  index: 2,
				  text: userInfo.unreadMessage
				})
			}else{
				uni.removeTabBarBadge({
					index: 2,
				})
			}
		},
		onPullDownRefresh() {
			this.page = 1
			this.getConversationList()
		},
		onReachBottom() {
			console.log('上拉加载更多')
			if(this.page == 0) {
				return
			}
			this.getConversationList()
		},
		methods:{
			conversationCellTap(item, index){
				console.log('会话内容', item)
				if(item.conversationType == 'system'){
					this.$Router.push({
						name: 'systemMessage',
						params: {
							conversationId: item.id,
							nickname: '系统消息'
						}
					})
				}else{
					this.$Router.push({
						name: 'chatMessage',
						params: {
							conversationId: item.id,
							nickname: item.userInfo.nickname,
							userId: item.userInfo.id
						}
					})
				}
			},
			//获取会话列表
			getConversationList(){
				message.getConversationList({
					page: this.page,
					size: this.size
				}).then(res => {
					uni.stopPullDownRefresh();
					if (res.data.code == 10000) {
						if(this.page == 1){
							this.conversationList = []
						}
						this.conversationList = [...this.conversationList, ...res.data.data.list]
						this.showEmpty = this.conversationList.length<1
						if (res.data.data.next) {
							this.page += 1
						}else{
							this.page = 0
						}
					}
				})
			},
		},
	}
</script>

<style lang="scss" scoped>
	.container{
		background-color: #FFFFFF;
		height: 100vh;
	}
</style>